<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >

                    <el-form-item label="订单编号">
                        <el-input v-model="baTable.form.items!.out_trade_no" disabled></el-input>
                    </el-form-item>

                    <el-form-item label="会员卡名称">
                        <el-input v-model="baTable.form.items!.vip_name.name" disabled></el-input>
                    </el-form-item>

                    <FormItem label="会员卡号" type="string" v-model="baTable.form.items!.vip_code"
                              prop="vip_code"
                              placeholder="请输入会员卡号"/>

                    <FormItem label="快递名称" type="remoteSelect"
                              v-model="baTable.form.items!.express_name" prop="express_name"
                              :input-attr="{ pk: 'ba_express.delivery_id', field: 'delivery_name', remoteUrl: '/admin/express/index' }"
                              placeholder="请选择快递名称"/>

                    <FormItem label="快递单号" type="string" v-model="baTable.form.items!.express_nums"
                              prop="express_nums"
                              placeholder="请填写快递单号"/>



                    <FormItem label="备注" type="textarea" v-model="baTable.form.items!.notes" :input-attr="{ showWordLimit: true, maxlength: 30, rows: 3 }" />

                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="onsubmit(formRef)"
                           type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const {t} = useI18n()

const onsubmit = (formRef) => {
    baTable.form.items.status = 2
    baTable.onSubmit(formRef)
}



const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    express_name: [buildValidatorData({name: 'required', title: '快递名称'})],
    express_nums: [buildValidatorData({name: 'required', title: '快递编号'})],
    vip_code: [buildValidatorData({name: 'required', title: '会员卡号'})],
})
</script>

<style scoped lang="scss"></style>
